STM32F4 两通道CAN通讯int main(void)
{
u8 key;
u8 i=0,t=0;
u8 cnt=0;
u8 canbuf[8];
u8 res1,res2;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(115200); //初始化串口波特率为115200
LED_Init(); //初始化LED
LCD_Init(); //LCD初始化
KEY_Init(); //按键初始化
CAN1_Mode_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,300,CAN_Mode_Normal);//CAN初始化环回模式,波特率500Kbps
CAN2_Mode_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,300,CAN_Mode_Normal);
POINT_COLOR=RED;//设置字体为红色
LCD_ShowString(30,50,200,16,16,"Explorer STM32F4");
LCD_ShowString(30,70,200,16,16,"CAN TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200,16,16,"2014/5/7");
LCD_ShowString(30,130,200,16,16,"Nnormal Mode");
LCD_ShowString(30,150,200,16,16,"KEY0:Send WK_UP:Mode");//显示提示信息
POINT_COLOR=BLUE;//设置字体为蓝色
LCD_ShowString(30,170,200,16,16,"Count:"); //显示当前计数值
LCD_ShowString(30,190,200,16,16,"Send Data:"); //提示发送的数据
LCD_ShowString(30,250,200,16,16,"Receive Data:"); //提示接收到的数据
while(1)
{
key=KEY_Scan(0);
if(key==KEY0_PRES)//KEY0按下,发送一次数据
{
for(i=0;i<8;i )
{
canbuf[i]=cnt i;//填充发送缓冲区
if(i<4)LCD_ShowxNum(30 i*32,210,canbuf[i],3,16,0X80); //显示数据
else LCD_ShowxNum(30 (i-4)*32,230,canbuf[i],3,16,0X80); //显示数据
}
res1=CAN1_Send_Msg(canbuf,8);//发送8个字节
res2=CAN2_Send_Msg(canbuf,8);
if(res2&&res1)LCD_ShowString(30 80,190,200,16,16, "1:Failed 2:Failed"); //提示发送失败
if((!res1)&&(!res2)) LCD_ShowString(30 80,190,200,16,16, "1:OK 2:OK"); //提示发送成功
if((!res1)&res2) LCD_ShowString(30 80,190,200,16,16, "1:Failed 2:OK");
if((!res2)&res1) LCD_ShowString(30 80,190,200,16,16, "1:OK 2:Failed");
}
t ;
delay_ms(10);
if(t==20)
{
LED0=!LED0;//提示系统正在运行
t=0;
cnt ;
LCD_ShowxNum(30 48,170,cnt,3,16,0X80); //显示数据
}
}
}
评论